<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'comSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('xmwms.outboundOrder.quick Search Fields') })"
        >
            <el-tooltip :content="t('Edit selected row')" placement="top">
                <el-button
                    v-blur
                    @click="onAction('edit')"
                    :disabled="baTable.table.selection!.length > 0 ? false:true"
                    class="table-header-operate"
                    type="primary"
                >
                    <Icon name="fa fa-pencil" />
                    <span class="table-header-operate-text">{{ t('Edit') }}</span>
                </el-button>
            </el-tooltip>

            <el-popconfirm
                @confirm="onAction('delete')"
                :confirm-button-text="t('Delete')"
                :cancel-button-text="t('Cancel')"
                confirmButtonType="danger"
                :title="t('Are you sure to delete the selected record?')"
                :disabled="baTable.table.selection!.length > 0 ? false:true"
            >
                <template #reference>
                    <div class="mlr-12">
                        <el-tooltip :content="t('Delete selected row')" placement="top">
                            <el-button v-blur :disabled="baTable.table.selection!.length > 0 ? false:true" class="table-header-operate" type="danger">
                                <Icon name="fa fa-trash" />
                                <span class="table-header-operate-text">{{ t('Delete') }}</span>
                            </el-button>
                        </el-tooltip>
                    </div>
                </template>
            </el-popconfirm>

            <el-button
                v-blur
                :disabled="baTable.table.selection!.length > 0 ? false:true"
                class="table-header-operate"
                type="success"
                @click="shipments"
            >
                <Icon color="#ffffff" name="el-icon-Select" />
                <span class="table-header-operate-text">{{ t('xmwms.outboundOrder.shipment') }}</span>
            </el-button>
        </TableHeader>

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />

        <!-- 表单 -->
        <PopupForm />

        <Detail />

        <Shipment />
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { cloneDeep, concat } from 'lodash-es'
import Detail from './detail.vue'
import Shipment from './shipment.vue'
import { ElNotification } from 'element-plus'
import { getShipmentData } from '/@/api/backend/xmwms/outboundOrder'

const { t } = useI18n()
const tableRef = ref()

let optButtons: OptButton[] = [
    {
        render: 'tipButton',
        name: 'detail',
        title: 'xmwms.outboundOrder.detail',
        text: '',
        type: 'warning',
        icon: 'fa fa-search-plus',
        class: 'table-row-info',
        disabledTip: false,
    },
    {
        render: 'tipButton',
        name: 'shipment',
        title: 'xmwms.outboundOrder.shipment',
        text: '',
        type: 'success',
        icon: 'fa fa-check',
        class: 'table-row-success',
        disabledTip: false,
        // 按钮是否禁用，请返回布尔值
        disabled: (row: TableRow, field: TableColumn) => {
            if (row.audit_status == 1) {
                return false
            } else {
                return true
            }
        },
    },
    {
        render: 'tipButton',
        name: 'edit',
        title: 'Edit',
        text: '',
        type: 'primary',
        icon: 'fa fa-pencil',
        class: 'table-row-info',
        disabledTip: false,
    },
]
optButtons = concat(optButtons, defaultOptButtons(['delete']))
// 给第一个按钮加上点击事件
optButtons[0].click = (row: TableRow, field: TableColumn) => {
    if (!row) return
    // 数据来自表格数据,未重新请求api,深克隆,不然可能会影响表格
    let rowClone = cloneDeep(row)
    baTable.form.extend!['detail'] = rowClone
    baTable.form.operate = 'Detail'
}
// 给第二个按钮加上点击事件
optButtons[1].click = (row: TableRow, field: TableColumn) => {
    if (!row) return
    baTable.form.extend!['shipment'] = []
    baTable.form.operate = 'shipment'
    shipmentData(row.id)
}
optButtons[2].click = (row: TableRow, field: TableColumn) => {
    if (!row) return
    // 数据来自表格数据,未重新请求api,深克隆,不然可能会影响表格
    let rowClone = cloneDeep(row)
    if (rowClone.audit_status == 1) {
        ElNotification({
            message: '已审核不能修改',
            type: 'error',
        })
        return
    }
    baTable.form.items = rowClone
    baTable.form.items.product_detail = JSON.parse(rowClone.product_detail)
    baTable.form.operate = 'Edit'
}

const baTable = new baTableClass(
    new baTableApi('/admin/xmwms.OutboundOrder/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            {
                label: t('xmwms.outboundOrder.outbound_no'),
                prop: 'outbound_no',
                align: 'center',
                width: '150',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
            },
            {
                label: t('xmwms.outboundOrder.order_sn'),
                prop: 'order_sn',
                align: 'center',
                width: '150',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
            },
            {
                label: t('xmwms.outboundOrder.wms_customer__name'),
                prop: 'wmsCustomer.name',
                width: '170',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                replaceValue: {},
            },
            {
                label: t('xmwms.outboundOrder.type'),
                prop: 'type',
                align: 'center',
                width: '130',
                render: 'tag',
                operator: '=',
                sortable: false,
                replaceValue: {
                    10: t('xmwms.outboundOrder.type 10'),
                    20: t('xmwms.outboundOrder.type 20'),
                    30: t('xmwms.outboundOrder.type 30'),
                },
            },
            {
                label: t('xmwms.outboundOrder.audit_status'),
                prop: 'audit_status',
                align: 'center',
                width: '130',
                render: 'tag',
                custom: { 0: 'info', 1: 'warning' },
                operator: '=',
                sortable: false,
                replaceValue: { 0: t('xmwms.outboundOrder.audit_status 0'), 1: t('xmwms.outboundOrder.audit_status 1') },
            },
            {
                label: t('xmwms.outboundOrder.shipment_status'),
                prop: 'shipment_status',
                align: 'center',
                width: '130',
                render: 'tag',
                custom: { 0: 'info', 1: 'success' },
                operator: '=',
                sortable: false,
                replaceValue: {
                    0: t('xmwms.outboundOrder.shipment_status 0'),
                    1: t('xmwms.outboundOrder.shipment_status 1'),
                },
            },
            {
                label: t('xmwms.outboundOrder.remark'),
                prop: 'remark',
                align: 'center',
                width: '170',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
            },
            {
                label: t('xmwms.outboundOrder.admin__username'),
                prop: 'admin.username',
                width: '150',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'tags',
                operator: 'LIKE',
                replaceValue: {},
            },
            {
                label: t('xmwms.outboundOrder.createtime'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('xmwms.outboundOrder.updatetime'),
                prop: 'update_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('Operate'),
                align: 'center',
                width: 170,
                render: 'buttons',
                buttons: optButtons,
                operator: false,
                fixed: 'right',
            },
        ],
        dblClickNotEditColumn: ['all'],
    },
    {
        defaultItems: { type: '10', product_detail: [] },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})

const onAction = (event: string) => {
    let selectionData = baTable.table.selection![0]
    if (selectionData && selectionData.audit_status == 1 && event == 'edit') {
        ElNotification({
            message: '已审核不能修改',
            type: 'error',
        })
        return
    }
    baTable.onTableHeaderAction(event, selectionData)
}

const shipments = () => {
    let ids = baTable.getSelectionIds()
    if (ids.length > 1) {
        ElNotification({
            message: '请选择一行发货',
            type: 'error',
        })
        return
    }
    let selectionData = baTable.table.selection![0]
    if (selectionData && selectionData.audit_status == 0) {
        ElNotification({
            message: '请审核后再发货',
            type: 'error',
        })
        return
    }
    baTable.form.extend!['shipment'] = []
    baTable.form.operate = 'shipment'
    shipmentData(ids.join(''))
}

const shipmentData = (id: string) => {
    getShipmentData(id)
        .then((res) => {
            baTable.form.extend!['shipment'] = res.data.row
            baTable.form.loading = false
        })
        .finally(() => {
            baTable.form.extend!.submitLoading = false
        })
}
</script>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
    name: 'xmwms/OutboundOrder',
})
</script>

<style scoped lang="scss">
.table-header-operate {
    margin-left: 12px;
}
</style>
